<?php
define("_ROOT",".");
$site_url = "http://xna.spvrk.com";
include "mainfile.php";
$file = "sitemap.xml";
//setting
//更新频率
$site_freg = "weekly";//"always", "hourly", "daily", "weekly", "monthly", "yearly" and "never".
$cate_freg = "weekly";
//最后更新 "filedate":目录最后更新日期,"now":访问时间,或者指定日期
$lastmodify = "filedate";
switch($lastmodify){
case "now":$lastmod = date("Y-m-d");break;
case "filedate":
	$mtime = filemtime(_ROOT."/cache/category.cache.php");
	$lastmod = date("Y-m-d",$mtime);
break;
default:
	$lastmod = $lastmodify;
}
$str =  '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
   <url>
      <loc>'.$site_url.'</loc>
      <lastmod>'.$lastmod.'</lastmod>
      <changefreq>'.$site_freg.'</changefreq>
      <priority>0.8</priority>
   </url>';
	$adb=$PlusDB->prefix(articles);
	$cdb = $PlusDB->prefix(category);
$sql="select *,n.ctime as ctime,n.id as id  from $xnanewsdb n left join $xnadb x on (x.id=n.site_id) where 1 $arg order by n.ctime desc ";
$rt=$PlusDB->SelectLimit($sql);
while($row=$rt->fetchRow()){
	$url=$site_url."/".str_replace("&","&amp;",getArticleUrl($row["id"],$row["cid"],$row["ctime"]));
	$url = str_replace("./view.php","view.php",$url);
	//die($url);
   $str.='<url>
      <loc>'.$url.'</loc>
	  <lastmod>'.date("Y-m-d").'</lastmod>
      <changefreq>'.$cate_freg.'</changefreq>
	  <priority>0.8</priority>
   </url>';
}
$str.= '</urlset>';
$fp=fopen($file,w);
@fputs($fp,$str);
fclose($fp);
$sitemap = PLUS_URL."/".$file;
echo "sitemap 地址 ".$sitemap."<br>";
echo "<a href=".$sitemap.">访问</a>";

	function getArticleUrla($aid,$cid,$ctime,$act="no"){
			global $site_conf,$catetree;
						if ($site_conf[system][outhtml] == "yes")
						{
							//ini
							$sroot=$this_cate_path="";
							//html设定
							$html_path = $site_conf[system][html_path];
							$html_ext = $site_conf[system][html_ext];
							//根目录路径
							$catestr = $catetree[$cid]["catestr"].",".$cid.",";
							$catestrs = explode(",",$catestr);
							foreach($catestrs as $cateid){
								if($cateid==0) continue;
								if($cateid>0) $this_cate_path.=$catetree[$cateid]["english_name"]."/";
							}
							$sroot .="..";
							//时间路径
							$ddtime = explode(" ", $ctime);
							$dtime = str_replace("-", "", $ddtime[0]);
							//文章路径
							$aurl = $html_path.$this_cate_path . $dtime . "/" . $cid . "," . $aid . ",0." . $html_ext;
						}
						else
						{
							$aurl= PLUS_ROOT_PATH."/view.php?cid=$cid&action=Article&aid=$aid";
						}
						return $aurl;
	}
?>